Skip to content

gh-139871: Fix 3.15 bytearray.take_bytes example#149520

Open
cmaloney wants to merge 1 commit intopython:mainfrom
cmaloney:cmaloney/take_bytes_broken_example
Open

gh-139871: Fix 3.15 bytearray.take_bytes example#149520
cmaloney wants to merge 1 commit intopython:mainfrom
cmaloney:cmaloney/take_bytes_broken_example

Conversation

@cmaloney
Copy link
Copy Markdown
Contributor

@cmaloney cmaloney commented May 7, 2026

Currently:

buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = bytes(buffer[:n + 1])
del buffer[:n + 1]
assert data == b'abc'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    assert data == b'abc'
           ^^^^^^^^^^^^^^
AssertionError

Adding in the newline both match:

buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = bytes(buffer[:n + 1])
del buffer[:n + 1]
assert data == b'abc\n'
assert buffer == bytearray(b'def')

buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = buffer.take_bytes(n + 1)
assert data == b'abc\n'
assert buffer == bytearray(b'def')

Currently:
```python
buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = bytes(buffer[:n + 1])
del buffer[:n + 1]
assert data == b'abc'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    assert data == b'abc'
           ^^^^^^^^^^^^^^
AssertionError
```

Adding in the `\n` makes the two match:

```python
buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = bytes(buffer[:n + 1])
del buffer[:n + 1]
assert data == b'abc\n'
assert buffer == bytearray(b'def')

buffer = bytearray(b'abc\ndef')
n = buffer.find(b'\n')
data = buffer.take_bytes(n + 1)
assert data == b'abc\n'
assert buffer == bytearray(b'def')
```
@cmaloney cmaloney added the 3.15 pre-release feature fixes, bugs and security fixes label May 7, 2026
@cmaloney cmaloney requested a review from AA-Turner as a code owner May 7, 2026 23:53
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels May 7, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Docs PRs May 7, 2026
@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32595407 | 📁 Comparing f69ca1c against main (49918f5)

  🔍 Preview build  

1 file changed
± whatsnew/3.15.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.15 pre-release feature fixes, bugs and security fixes awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant